package com.dongge.action;

import com.dongge.entity.Right;
import com.dongge.service.RightService;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2015/11/3.
 */
@Controller
@Scope("prototype")
public class RightAction extends BaseAction<Right> implements CURDAction{

    private Long parentId;

    @Resource
    private RightService rightService;

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String add() {
        Right parent = rightService.get(parentId);
        model.setParent(parent);
        rightService.save(model);
        return "toList";
    }

    public String addPrepare() {
        List<Right> rights = rightService.getAll();
        ActionContext.getContext().put("rights", rights);
        return "addPrepare";
    }

    public String delete() {
        rightService.delete(model.getId());
        return "toList";
    }

    public String edit() {
        Right parent = rightService.get(parentId);
        Right right = rightService.get(model.getId());
        right.setName(model.getName());
        right.setUrl(model.getUrl());
        right.setParent(parent);
//        rightService.save(right);
        rightService.update(right);
        return "toList";
    }

    public String editPrepare() {
        List<Right> rights = rightService.getAll();
        ActionContext.getContext().put("rights", rights);
        Right right = rightService.get(model.getId());
        ActionContext.getContext().getValueStack().push(right);
        return "editPrepare";
    }

    public String list() {
        List<Right> rights = rightService.findRightsByParentId(parentId);
        ActionContext.getContext().put("rights",rights);
        return "list";
    }
}
